package com.zq.practice;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @ProjectName: ybxy6
 * @Titile: StudentManage
 * @Author: zqsfg
 * @Description: TODO
 */
public class StudentManage {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("1","zq",18,"宜宾"));
        students.add(new Student("2","zxc",19,"荆州"));
        students.add(new Student("3","ysq",20,"北京"));
        students.add(new Student("4","zxy",21,"上海"));
        students.add(new Student("5","zq",18,"四川"));
        students.add(new Student("6","ss",18,"武汉"));
        students.add(new Student("7","mm",18,"北京"));
        students.add(new Student("8","aa",18,"西安"));
        students.add(new Student("9","dc",18,"上海"));
        students.add(new Student("10","ad",18,"南充"));
        students.add(new Student("11","mj",18,"上海"));
        Scanner scanner = new Scanner(System.in);
        int choice;
        //利用循环返回主界面
        do {
            // 显示菜单
            System.out.println("\n学生管理系统");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生信息");
            System.out.println("4. 查询学生");
            System.out.println("5. 退出");
            System.out.print("请输入你的选择: ");
            choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.println("1. 添加学生");
                    addStudent(students);
                    break;
                case 2:
                    System.out.println("2. 删除学生");

                    deleteStudent(students, scanner);
                    break;
                case 3:
                    System.out.println("3. 修改学生信息");
                    updateStudent(students, scanner);
                    break;
                case 4:
                    System.out.println("4. 查询学生");
                    queryStudent(students, scanner);
                    break;
                case 5:
                    System.out.println("5. 退出");
                    System.out.println("感谢你的使用，再见");
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        } while (choice != 5);




    }
    //1.添加学生
    public static void addStudent(  List<Student> students){
        //创建学生集合
        System.out.println("请输入学生信息：");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号：");
        String id= scanner.next();
        //判断学号是否存在(学号具有唯一性)
        if (isStudentExist(students, id)) {
            System.out.println("学号已存在，请重新输入");
            return;
        }
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        System.out.println("请输入居住地");
        String address = scanner.next();
        Student s = new Student(id,name,age,address);
        //添加数据到集合中
        students.add(s);
        System.out.println("添加成功");

    }
    // 2. 删除学生（按学号）
    private static void deleteStudent(List<Student> students, Scanner scanner) {
        System.out.print("请输入要删除的学生学号: ");
        String id = scanner.nextLine();

        // 查找学生
        Student studentById = findStudentById(students, id);
        if (studentById == null) {
            System.out.println("未找到该学号对应的学生！");
            return;
        }
//        int index = findStudentIndex(students, id);
//        if (index == -1) {
//            System.out.println("错误：未找到该学号对应的学生！");
//            return;
//        }

        // 删除学生
        students.remove(studentById);
        System.out.println("学生删除成功！");
    }

    // 3. 修改学生信息（按学号）
    private static void updateStudent(List<Student> students, Scanner scanner) {
        System.out.print("请输入要修改的学生学号: ");
        String id = scanner.nextLine();

        // 查找学生索引
        int index = findStudentIndex(students, id);
        if (index == -1) {
            System.out.println("错误：未找到该学号对应的学生！");
            return;
        }

        Student student = students.get(index);
        System.out.println("\n当前学生信息：");
        System.out.println(student); // 显示原信息
        System.out.println("请输入新的学生信息：");
        System.out.print("请输入新学号（原学号：" + student.getId() + "）: ");
        String newId = scanner.nextLine();

// 检查新学号是否存在，且不等于原学号
        if (!newId.equals(student.getId()) && isStudentExist(students, newId)) {
            System.out.println("错误：该学号已被其他学生使用！");
            return;
        }
        student.setId(newId);
        System.out.print("请输入新姓名（原姓名：" + student.getName() + "）: ");
        student.setName(scanner.nextLine());
        System.out.print("请输入新年龄（原年龄：" + student.getAge() + "）: ");
        student.setAge(scanner.nextInt());
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入新住址（原住址：" + student.getAddress() + "）: ");
        student.setAddress(scanner.nextLine());


        System.out.println("学生信息修改成功！");
    }

    // 4. 查询学生（支持按学号查询和查看所有）
    private static void queryStudent(List<Student> students, Scanner scanner) {
        System.out.println("\n查询方式选择：");
        System.out.println("1. 按学号查询单个学生");
        System.out.println("2. 查看所有学生信息");
        System.out.print("请选择查询方式（1/2）: ");
        int queryChoice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        if (queryChoice == 1) {
            System.out.print("请输入要查询的学号: ");
            String id = scanner.nextLine();
            Student student = findStudentById(students, id);
            if (student == null) {
                System.out.println("未找到该学号对应的学生！");
            } else {
                System.out.println("查询结果：");
                System.out.println("学号\t姓名\t年龄\t住址");
                System.out.printf("%s\t%s\t%d\t%s%n",
                        student.getId(), student.getName(), student.getAge(), student.getAddress());

            }
        } else if (queryChoice == 2) {
            if (students.isEmpty()) {
                System.out.println("暂无学生信息，请先添加学生！");
                return;
            }

            // 按学号排序
            List<Student> sortedStudents = new ArrayList<>(students);
            sortedStudents.sort((s1, s2) -> {
                try {
                    int num1 = Integer.parseInt(s1.getId());
                    int num2 = Integer.parseInt(s2.getId());
                    return Integer.compare(num1, num2);
                } catch (NumberFormatException e) {
                    return s1.getId().compareTo(s2.getId());
                }
            });

            System.out.println("\n所有学生信息列表：");
            System.out.println("学号\t姓名\t年龄\t住址");
            System.out.println("-----------------------------");
            // 遍历排序后的集合
            for (Student student : sortedStudents) {
                System.out.printf("%s\t%s\t%d\t%s%n",
                        student.getId(), student.getName(), student.getAge(), student.getAddress());
            }
        } else {
            System.out.println("输入错误：请选择1或2！");
        }
    }

    // 检查学生是否存在（学号是否重复）
    private static boolean isStudentExist(List<Student> students, String id) {
        return findStudentIndex(students, id) != -1;
    }

    // 根据学号查找学生索引（存在返回索引，不存在返回-1）
    private static int findStudentIndex(List<Student> students, String id) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    // 根据学号查找学生对象（存在返回对象，不存在返回null）
    private static Student findStudentById(List<Student> students, String id) {
        //
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }


    }

